package water.com.Log;

import lombok.Data;
import water.com.Observer.Subject;

@Data
public class Log {

    private volatile static Log log;
    Subject logSubject;
    //存储日志信息
    private String logString;
    private String logLevel = "logInfo";
    private final String INFO = "logInfo" ,DEBUG = "debug",ERROR = "error";

    public Subject getLogSubject(){
        return logSubject;
    }

    public void setLogSubject(Subject logSubject){
        this.logSubject = logSubject;
    }

    private Log(){}

    public void setLogLevel(String logLevel) throws Exception {
        if(!logLevel.equals(INFO)&&!logLevel.equals(DEBUG)&&!logLevel.equals(ERROR)){
            throw new Exception("日志级别设置错误，请重新设置: logInfo,debug,error");
        }
        this.logLevel = logLevel;
    }

    public void debug(String logStr){
        this.logString = logStr;
        if(logLevel.equals(INFO)||logLevel.equals(DEBUG)){
            logSubject.notifyObservers();
        }
    }

    public void info(String logStr){
        this.logString = logStr;
        if(logLevel.equals(INFO)){
            logSubject.notifyObservers();
        }
    }

    public void error(String logStr){
        this.logString = logStr;
        if(logLevel.equals(INFO)||logLevel.equals(DEBUG)||logLevel.equals(ERROR)){
            logSubject.notifyObservers();
        }
    }

    public static Log getLog(){
        if(log==null){
            synchronized (Log.class){
                if(log==null){
                    log = new Log();
                }
            }
        }
        return log;
    }

}
